package ps.server.trigger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Vector;

import ps.net.TriggerDescContent;


public class TriggerManager {
    
    private static String DEF_SAVE_FILE_NAME = "trigger.bin";
    
    TriggerEntry[] triggers = new TriggerEntry[0];
    Vector<TriggerEntry> triggerList = new Vector<TriggerEntry>();
    int nextTriggerId = 0;
    
    private String saveFileName;

    public TriggerManager() {
        this(DEF_SAVE_FILE_NAME);
    }
    
    public TriggerManager(String saveFileName) {
        this.saveFileName = saveFileName;
        load();
        triggers = createTriggerArray();
//        save();
    }
    
    public void addTrigger(TriggerEntry entry) {
        if (triggerList.contains(entry)) {
            triggerList.remove(entry);
        }
        entry.setId(nextTriggerId);
        nextTriggerId++;
        triggerList.add(entry);
        triggers = createTriggerArray();
        save();
    }
    
    public void addTriggers(TriggerEntry[] entries) {
        for (TriggerEntry entry : entries) {
            if (triggerList.contains(entry)) {
                triggerList.remove(entry);
            }
            entry.setId(nextTriggerId);
            nextTriggerId++;
            triggerList.add(entry);
        }
        triggers = createTriggerArray();
        save();
    }
    
    public void removeTrigger(TriggerEntry entry) {
        triggerList.remove(entry);
        triggers = createTriggerArray();
        save();
    }
    
    public TriggerEntry getTriggerByTitle(String str) {
        for (TriggerEntry entry : triggers) {
            if (entry.getTitle().equalsIgnoreCase(str)) {
                return entry;
            }
        }
        return null;
    }
    
    public TriggerEntry getTriggerById(int id) {
        for (TriggerEntry entry : triggers) {
            if (entry.getId() == id) {
                return entry;
            }
        }
        return null;
    }
    
    private TriggerEntry[] createTriggerArray() {
        return triggerList.toArray(new TriggerEntry[triggerList.size()]);
    }
    
    public TriggerEntry[] getAllTrigger() {
        return triggers;
    }
    
    public void setTriggerEntries(TriggerEntry[] entries) {
        triggerList.clear();
        for (int i = 0; i < entries.length; i++) {
            entries[i].setId(nextTriggerId);
            nextTriggerId++;
            triggerList.add(entries[i]);
        }
        triggers = createTriggerArray();
    }
    
    public void save() {
        try {
            FileOutputStream out = new FileOutputStream(saveFileName);
            TriggerDescContent cont = new TriggerDescContent(TriggerDescContent.CMD_ADD, triggers);
            cont.writeContent(out);
            out.flush();
            out.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    private void load() {
        try {
            if ((new File(saveFileName)).exists()) {
                TriggerDescContent cont = new TriggerDescContent(TriggerDescContent.CMD_ADD, new TriggerEntry[0]);
                FileInputStream in = new FileInputStream(saveFileName);
                
                cont.readContent(in);
                in.close();
                
                setTriggerEntries(cont.getTriggerEntries());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
